var PREFS = require('/Prefs');

var HSInfoView = {};
HSInfoView.Prefs = HSInfoView.Prefs || {};
HSInfoView.Prefs.WIDTH = 420;

HSInfoView.Prefs.HEIGHT = 324;
// 5

//HSInfoView.Prefs.HEIGHT = 324-32; // 4

HSInfoView.Prefs.HSICONSIZE = 32;
HSInfoView.Prefs.HSICONDISTANCE = 5;
HSInfoView.Prefs.HSICONLEFT = 5;
HSInfoView.Prefs.HSICONTOP = 5;
HSInfoView.Prefs.HSDISTANCEBOXWIDTH = 60;
HSInfoView.Prefs.HSDISTANCEBOXHIGHT = HSInfoView.Prefs.HSICONSIZE;
HSInfoView.Prefs.HSTITLELEFTPADDING = 5;
HSInfoView.Prefs.HSLINIENICONWIDTH = 54;
//
HSInfoView.Prefs.LinienTyp_BUS = 1 << 1;
HSInfoView.Prefs.LinienTyp_SBAHN = 1 << 2;
HSInfoView.Prefs.LinienTyp_UBAHN = 1 << 3;
HSInfoView.Prefs.LinienTyp_ZAHNRAD = 1 << 4;

function WSCaller(pAnnotation, pHalteStellenIDsArray, pHalteStelleType, pHalteStelleTitle, pHalteStelleDistanceM) {

	this.URL = createURL(pHalteStellenIDsArray, pHalteStelleType, 0);
	Ti.API.info("Set URL:" + this.URL);
	this.lastRefreshAbfahrtsLines = 0;
	this.HalteStellenA = pHalteStellenIDsArray;
	this.HalteStelleType = pHalteStelleType;
	this.HalteStelleTitle = pHalteStelleTitle || "Unbekannt";
	this.HalteStelleDistanceM = pHalteStelleDistanceM;

	this.RefreshButton = {};
	this.AbsolutzeitButton = {};
	this.VorherigeZeitButton = {}; 
	this.NaechsteZeitButton = {};
	this.ResetZeitButton = {};
	this.PinButton = {};
	
	this.ANNOVIEW = Titanium.UI.createView({
		/*
		 borderRadius:10,
		 borderColor: 'yellow',
		 borderWidth: 10,
		 */
		backgroundColor : '#333333',
		width : HSInfoView.Prefs.WIDTH,
		top : 20,
		left : 20,
		height : HSInfoView.Prefs.HEIGHT
	}); 


	this.BUTTONBARVIEW = {};

	this.ANNOTATION = pAnnotation;
	
	this.IsRelativAbfahrtzeit = true;
	this.AbfahrtzeitOffset = 0

	this.ANNOTATION.setLeftView(this.ANNOVIEW);

	this.isBus = (pHalteStelleType & HSInfoView.Prefs.LinienTyp_BUS) > 0;
	this.isUBahn = (pHalteStelleType & HSInfoView.Prefs.LinienTyp_UBAHN ) > 0;
	this.isSBahn = (pHalteStelleType & HSInfoView.Prefs.LinienTyp_SBAHN) > 0;
	this.isZRadBahn = (pHalteStelleType & HSInfoView.Prefs.LinienTyp_ZAHNRAD) > 0;

	this.loadingScreen = createLoadingScreeen();

	this.ANNOVIEW.add(this.loadingScreen);

	this.callBack = {

		Refback : this,
		// function called when the response data is available
		onload : function(e) {
			Ti.API.info("Enter HTTPClient.onLoad. Received text: " + this.responseText);

			this.Refback.createAnnotation(this.responseText);
		},
		// function called when an error occurs, including a timeout
		onerror : function(e) {
			Ti.API.debug(e.error);
			this.Refback.ANNOVIEW.remove(this.Refback.loadingScreen)

		},
		timeout : 25000 // in milliseconds
	}

	this.client = Ti.Network.createHTTPClient(this.callBack);
}

/*
 **************************************************
 **************************************************
 *
 *   refresh
 *
 **************************************************/
WSCaller.prototype.refresh = function(e, a) {

	Titanium.API.info("You clicked the button. Enter Refresh.call");

	a = a || false;
	var AnnoObject = this.THIS;
	if (a === true)
		AnnoObject = e;

	AnnoObject.RefreshButton.removeEventListener('click', AnnoObject.refresh);

	AnnoObject.ANNOVIEW.remove(AnnoObject.SCROLLVIEW);
	AnnoObject.loadingScreen = createLoadingScreeen();
	AnnoObject.ANNOVIEW.add(AnnoObject.loadingScreen);
	AnnoObject.URL = createURL(AnnoObject.HalteStellenA, AnnoObject.HalteStelleType, AnnoObject.AbfahrtzeitOffset);
	AnnoObject.client = Ti.Network.createHTTPClient(AnnoObject.callBack);
	AnnoObject.client.open("GET", AnnoObject.URL);
	AnnoObject.client.send();
	Ti.API.info("Exit Refresh.call ");
	
	setTimeout(function() {
		AnnoObject.RefreshButton.addEventListener('click', AnnoObject.refresh)
	}, 2500);
	
}
/*
 **************************************************
 **************************************************
 *
 *   changeAbsolutRelativZeit
 *
 **************************************************/
WSCaller.prototype.changeAbsolutRelativZeit = function(e) {

	Titanium.API.info("You clicked the button. Enter changeAbsolutRelativZeit.call");
	var AnnoObject = this.THIS;
	AnnoObject.AbsolutzeitButton.removeEventListener('click', AnnoObject.changeAbsolutRelativZeit);
	AnnoObject.IsRelativAbfahrtzeit = !AnnoObject.IsRelativAbfahrtzeit;
	AnnoObject.refresh(AnnoObject, true);

	setTimeout(function() {
		AnnoObject.AbsolutzeitButton.addEventListener('click', AnnoObject.changeAbsolutRelativZeit)
	}, 2500);
}


/*
 **************************************************
 **************************************************
 *
 *   setVorherigeZeit
 *
 **************************************************/
WSCaller.prototype.setVorherigeZeit = function(e) {

	var AnnoObject = this.THIS;
	AnnoObject.VorherigeZeitButton.removeEventListener('click', AnnoObject.setVorherigeZeit);

	AnnoObject.AbfahrtzeitOffset = AnnoObject.AbfahrtzeitOffset - (8*60*1000);

	AnnoObject.refresh(AnnoObject, true);

	if(AnnoObject.AbfahrtzeitOffset==0)
		AnnoObject.ResetZeitButton.hide();
	else
		AnnoObject.ResetZeitButton.show();

	setTimeout(function() {
		AnnoObject.VorherigeZeitButton.addEventListener('click', AnnoObject.setVorherigeZeit)
	}, 2000);
}
/*
 **************************************************
 **************************************************
 *
 *   setNaechsteZeit
 *
 **************************************************/
WSCaller.prototype.setNaechsteZeit = function(e) {

	var AnnoObject = this.THIS;
	AnnoObject.NaechsteZeitButton.removeEventListener('click', AnnoObject.setNaechsteZeit);

    AnnoObject.AbfahrtzeitOffset = AnnoObject.AbfahrtzeitOffset + (8*60*1000);

	AnnoObject.refresh(AnnoObject, true);

	if(AnnoObject.AbfahrtzeitOffset==0)
		AnnoObject.ResetZeitButton.hide();
	else
		AnnoObject.ResetZeitButton.show();

	setTimeout(function() {
		AnnoObject.NaechsteZeitButton.addEventListener('click', AnnoObject.setNaechsteZeit)
	}, 2000);
}

/*
 **************************************************
 **************************************************
 *
 *   resetAzZeit
 *
 **************************************************/
WSCaller.prototype.resetAzZeit = function(e) {
	
	var AnnoObject = this.THIS;
	AnnoObject.ResetZeitButton.removeEventListener('click', AnnoObject.resetAzZeit);

    AnnoObject.AbfahrtzeitOffset = 0;

	AnnoObject.refresh(AnnoObject, true);

	if(AnnoObject.AbfahrtzeitOffset==0)
		AnnoObject.ResetZeitButton.hide();
	else
		AnnoObject.ResetZeitButton.show();

	setTimeout(function() {
		AnnoObject.ResetZeitButton.addEventListener('click', AnnoObject.resetAzZeit)
	}, 1500);

}



/**************************************************
 **************************************************
 *
 *   create
 *
 **************************************************/
WSCaller.prototype.create = function() {

	Ti.API.info("Enter WSCaller.call");
	this.createHeader();
	this.client.open("GET", this.URL);
	this.client.send();
	Ti.API.info("Exit WSCaller.call ");

};

WSCaller.prototype.createSkeleton = function() {

	this.createHeader();
	//this.createButtonLine();

};

/**************************************************
 **************************************************
 *
 *   createAbfahrtsLines
 *
 **************************************************/

WSCaller.prototype.createAbfahrtsLines = function() {

	Ti.API.info("createAbfahrtsLines");

	if (new Date().getTime() - this.lastRefreshAbfahrtsLines < (45 * 1000)) {
		Ti.API.info("createAbfahrtsLines -- skipped");
		return;
	}

	if (this.SCROLLVIEW != undefined) {
		this.ANNOVIEW.remove(this.SCROLLVIEW);
		delete this.SCROLLVIEW;
	}
	this.URL = createURL(this.HalteStellenA, this.HalteStelleType, this.AbfahrtzeitOffset);
	this.loadingScreen = createLoadingScreeen();
	this.ANNOVIEW.add(this.loadingScreen);

	this.client.open("GET", this.URL);
	this.client.send();
};

/**************************************************
 **************************************************
 *
 *   getView
 *
 **************************************************/
WSCaller.prototype.getView = function() {
	return this.ANNOVIEW;
};

/**************************************************
 **************************************************
 *
 *   createAnnotation
 *
 **************************************************/
WSCaller.prototype.createAnnotation = function(JSONStr1) {
	Ti.API.info("Enter createAnnotation, with Input:" + JSONStr1);

	var data1 = eval('(' + JSONStr1 + ')');

	Ti.API.info("Anzahl Zeilen Abfahrten:" + data1.length);

	/////////////////////////////////////////////////////////////////////////////////////

	var
	scrollviewheight;
	var offset = 0;
	if (data1.length >= 5) {
		scrollviewheight = 187;
		offset = 12;
	} else if (data1.length == 4) {
		scrollviewheight = 187 - 36;
		offset = 6;
	} else if (data1.length == 3) {
		scrollviewheight = 187 - 32 - 43;
		offset = 9;
	} else if (data1.length <= 2) {
		scrollviewheight = 187 - 32 - 32 - 45;
		offset = 7;
	}

	if (this.SCROLLVIEW != undefined) {
		this.ANNOVIEW.remove(this.SCROLLVIEW);
		delete this.SCROLLVIEW;
	}

	this.SCROLLVIEW = Ti.UI.createScrollView({
		contentWidth : 'auto',
		contentHeight : 'auto',
		showVerticalScrollIndicator : false,
		showHorizontalScrollIndicator : false,
		left : 0,
		top : 53,
		height : scrollviewheight,
		width : HSInfoView.Prefs.WIDTH - 3,
		backgroundColor : '#333333',
		scrollType : 'vertical'
	});

	this.createButtonLine(46 + offset + scrollviewheight);

	if (this.loadingScreen != undefined) {
		this.ANNOVIEW.remove(this.loadingScreen);
		delete this.loadingScreen;
	}

	this.ANNOVIEW.add(this.SCROLLVIEW);

	for (var i = 0; i < data1.length; i++) {

		var X = {};
		X.linienNr = data1[i].linienNr.toLowerCase();
		X.linienType = data1[i].linienType;
		X.gleis = data1[i].gleis;
		X.richtung = data1[i].richtung
		X.abfahrten = data1[i].abfahrten;

		/*
		 abfahrtzeit	"21:31"
		 abfahrtzeitOffset	"12"
		 abfahrtzeitRealTime	"21:31"
		 */
		this.createAbfahrtsInfoLine(this.SCROLLVIEW, i, X);

	}

	this.lastRefreshAbfahrtsLines = new Date().getTime();
}
/*
 **************************************************
 **************************************************
 *
 *   createButtonLine
 *
 **************************************************/

WSCaller.prototype.createButtonLine = function(pYPOS) {

	var ButtonBarHight = 76;
	var tmpView;

	pYPOS = pYPOS || 248;

	if (this.BUTTONBARVIEW != undefined && (this.BUTTONBARVIEW.hasOwnProperty('top') )) {

		if (this.BUTTONBARVIEW.top == pYPOS) {
			return;
		}

		var tmpView = Titanium.UI.createView({// no Bouncing tmpView
			backgroundColor : '#333333',
			width : 1,
			top : pYPOS,
			left : 0,
			height : ButtonBarHight,
			opacity : 0.0
		});

		this.ANNOVIEW.add(tmpView);

		var C = this.BUTTONBARVIEW.getChildren();
		for (var i = 0; (C != 'undefined' && i < C.length); i++) {
			this.BUTTONBARVIEW.remove(C[i]);
		}

		this.ANNOVIEW.remove(this.BUTTONBARVIEW);
		delete this.BUTTONBARVIEW;
	}

	this.BUTTONBARVIEW = Titanium.UI.createView({
		backgroundColor : '#333333',
		width : HSInfoView.Prefs.WIDTH,
		top : pYPOS,
		left : 0,
		height : ButtonBarHight,
	});

	var HSButtonLine = Ti.UI.createLabel({
		color : '#c8c8c8',
		top : 2,
		left : 0, //HSInfoView.Prefs.HSICONLEFT,
		width : HSInfoView.Prefs.WIDTH - HSInfoView.Prefs.HSICONLEFT,
		height : '2',
		borderColor : '#c8c8c8',
		borderWidth : 1
	});

	this.BUTTONBARVIEW.add(HSButtonLine);


	var dis = 14;
	this.RefreshButton = Titanium.UI.createButton({
		THIS : this,
		title : '',
		left : HSInfoView.Prefs.WIDTH - (57 + HSInfoView.Prefs.HSICONLEFT),
		top : 11,
		width : 57,
		height : 57,
		backgroundImage : 'images/button/refresh_57_57.png',
		backgroundSelectedImage : 'images/bus_32_32.png'

	});

	this.AbsolutzeitButton = Titanium.UI.createButton({
		THIS : this,
		title : '',
		left : 2+ 4*57 + 4*dis,
		top : 11,
		width : 57,
		height : 57,
		backgroundImage : 'images/button/absolutabfahrtzeit_57_57.png',
		backgroundSelectedImage : 'images/bus_32_32.png'

	});


	this.PinButton = Titanium.UI.createButton({
		THIS : this,
		title : '',
		left : 2+ 3*57 + 3*dis,
		top : 11,
		width : 57,
		height : 57,
		backgroundImage : 'images/button/pin_57_57.png',
		backgroundSelectedImage : 'images/bus_32_32.png'
	});


	this.NaechsteZeitButton = Titanium.UI.createButton({
		THIS : this,
		title : '',
		left : 2+ 2*57 + 2*dis,
		top : 11,
		width : 57,
		height : 57,
		backgroundImage : 'images/button/azforward_57_57.png',
		backgroundSelectedImage : 'images/bus_32_32.png'
	});
	
	this.ResetZeitButton = Titanium.UI.createButton({
		THIS : this,
		title : '',
		left : 2+ 57 + dis,
		top : 11,
		width : 57,
		height : 57,
		backgroundImage : 'images/button/azreset_57_57.png',
		backgroundSelectedImage : 'images/bus_32_32.png'
	});
	
	
	this.VorherigeZeitButton = Titanium.UI.createButton({
		THIS : this,
		title : '',
		left : 2 ,
		top : 11,
		width : 57,
		height : 57,
		backgroundImage : 'images/button/azback_57_57.png',
		backgroundSelectedImage : 'images/bus_32_32.png'
	});	


	this.RefreshButton.addEventListener('click', this.refresh);
	this.AbsolutzeitButton.addEventListener('click', this.changeAbsolutRelativZeit);

	this.VorherigeZeitButton.addEventListener('click', this.setVorherigeZeit); 
	this.NaechsteZeitButton.addEventListener('click', this.setNaechsteZeit);
	this.ResetZeitButton.addEventListener('click', this.resetAzZeit);


	this.BUTTONBARVIEW.add(this.RefreshButton);

	this.BUTTONBARVIEW.add(this.AbsolutzeitButton);

	this.BUTTONBARVIEW.add(this.VorherigeZeitButton);

	this.BUTTONBARVIEW.add(this.NaechsteZeitButton);
	
	this.BUTTONBARVIEW.add(this.ResetZeitButton);

	this.BUTTONBARVIEW.add(this.PinButton);

	this.ANNOVIEW.add(this.BUTTONBARVIEW);

	if(this.AbfahrtzeitOffset==0)
		this.ResetZeitButton.hide();
	else
		this.ResetZeitButton.show();

	if ((tmpView != undefined) && tmpView.hasOwnProperty('top')) {
		this.ANNOVIEW.remove(tmpView);
	}

}
/*
 **************************************************
 **************************************************
 *
 *   createHeader
 *
 **************************************************/
WSCaller.prototype.createHeader = function() {
	Ti.API.info("Enter createHeader");

	var IconPosfilled = 0;

	var hasDistance = ((this.HalteStelleDistanceM !== 'undefined') && ((this.HalteStelleDistanceM + '').replace(/\s+/, '').length > 0));

	if (this.isSBahn) {
		HSSBahnImage = Ti.UI.createImageView({
			image : 'images/sbahn_32_32.png',
			top : HSInfoView.Prefs.HSICONTOP,
			left : HSInfoView.Prefs.HSICONLEFT,
			height : 32,
			width : 32
		});
		IconPosfilled = 1;
	}

	if (this.isUBahn) {
		HSUBahnImage = Ti.UI.createImageView({
			image : 'images/ubahn_32_32.png',
			top : HSInfoView.Prefs.HSICONTOP,
			left : HSInfoView.Prefs.HSICONLEFT,
			height : 32,
			width : 32
		});

		if (IconPosfilled == 1) {
			HSUBahnImage.left = HSInfoView.Prefs.HSICONLEFT + HSInfoView.Prefs.HSICONSIZE + HSInfoView.Prefs.HSICONDISTANCE;
			IconPosfilled = 2;
		} else
			IconPosfilled = 1;

	}

	if (this.isBus) {
		HSBusImage = Ti.UI.createImageView({
			image : 'images/bus_32_32.png',
			top : HSInfoView.Prefs.HSICONTOP,
			left : HSInfoView.Prefs.HSICONLEFT,
			height : 32,
			width : 32
		});

		if (IconPosfilled == 0) {
			HSBusImage.left = HSInfoView.Prefs.HSICONLEFT;
			IconPosfilled = 1;
		} else {
			if (IconPosfilled == 1) {
				HSBusImage.left = HSInfoView.Prefs.HSICONLEFT + HSInfoView.Prefs.HSICONSIZE + HSInfoView.Prefs.HSICONDISTANCE;
				IconPosfilled = 2;
			} else {
				if (IconPosfilled == 2) {
					HSBusImage.left = HSInfoView.Prefs.HSICONLEFT + HSInfoView.Prefs.HSICONSIZE + HSInfoView.Prefs.HSICONDISTANCE + HSInfoView.Prefs.HSICONSIZE + HSInfoView.Prefs.HSICONDISTANCE;
					IconPosfilled = 3
				}
			}
		}
	}

	// HS Title

	var L = (IconPosfilled * HSInfoView.Prefs.HSICONSIZE) + ((IconPosfilled + 1) * HSInfoView.Prefs.HSICONDISTANCE);
	var W = HSInfoView.Prefs.WIDTH - (4 + L);
	if (hasDistance) {
		W = HSInfoView.Prefs.WIDTH - (HSInfoView.Prefs.HSDISTANCEBOXWIDTH + L + HSInfoView.Prefs.HSICONLEFT * 2);
	}

	var ScrollViewTitle = Ti.UI.createScrollView({
		contentWidth : 'auto',
		contentHeight : 'auto',
		showVerticalScrollIndicator : false,
		showHorizontalScrollIndicator : false,
		left : L,
		top : HSInfoView.Prefs.HSICONTOP + 1,
		height : 36,
		width : W,
		backgroundColor : '#333333',
		scrollType : 'horizontal'
	});

	var TitleLabel = Ti.UI.createLabel({
		wordWrap : false,
		color : 'white',
		font : {
			fontSize : 24,
			fontFamily : 'sans-serif',
			// fontWeight : 'bold'
		},
		text : this.HalteStelleTitle,
		textAlign : Ti.UI.TEXT_ALIGNMENT_LEFT,
		top : 0,
		left : 0,
		width : 'auto',
		height : 32,
	});

	ScrollViewTitle.add(TitleLabel);

	// Distanz
	var HSDistance = Ti.UI.createLabel({
		wordWrap : false,
		borderRadius : 4,
		backgroundColor : '#84848A',
		color : 'white',
		font : {
			fontSize : 16,
			fontFamily : 'sans-serif'
		},
		textAlign : Ti.UI.TEXT_ALIGNMENT_CENTER,
		top : HSInfoView.Prefs.HSICONTOP,
		left : (HSInfoView.Prefs.WIDTH - HSInfoView.Prefs.HSICONLEFT) - HSInfoView.Prefs.HSDISTANCEBOXWIDTH,
		width : HSInfoView.Prefs.HSDISTANCEBOXWIDTH,
		height : HSInfoView.Prefs.HSDISTANCEBOXHIGHT,
		html : formatHalteStelleDistanz(this.HalteStelleDistanceM)
	});

	var HSTitleLine = Ti.UI.createLabel({
		color : '#c8c8c8',
		top : 47,
		left : HSInfoView.Prefs.HSICONLEFT,
		width : HSInfoView.Prefs.WIDTH - 2 * HSInfoView.Prefs.HSICONLEFT,
		height : '2',
		borderColor : '#c8c8c8',
		borderWidth : 1
	});

	if (hasDistance) {
		this.ANNOVIEW.add(HSDistance);
	}

	if (this.isSBahn)
		this.ANNOVIEW.add(HSSBahnImage);

	if (this.isBus)
		this.ANNOVIEW.add(HSBusImage);

	if (this.isUBahn)
		this.ANNOVIEW.add(HSUBahnImage);

	this.ANNOVIEW.add(HSTitleLine);
	this.ANNOVIEW.add(ScrollViewTitle);

}

WSCaller.prototype.printHSIDs = function() {
	var ret = 'HSIDs: ';
	if (this.HalteStellenA != undefined) {
		for (var i = 0; i < this.HalteStellenA.length; i++) {
			ret = ret + "," + this.HalteStellenA[i];
		}
	}

	Ti.API.info(ret);
}
/*
 **************************************************
 **************************************************
 *
 *   createAbfahrtsInfoLine
 *
 **************************************************/

WSCaller.prototype.createAbfahrtsInfoLine = function(scrollView, i, LineRichtung) {

	var LineIconLV, LinienImage;
	var CntAbfahrtzeiten = 0;
	if (LineRichtung.abfahrten.length > 0)
		CntAbfahrtzeiten = 1;
	if (LineRichtung.abfahrten.length > 1)
		CntAbfahrtzeiten = 2;

	var HSTrainArrival1, HSTrainArrival2;

	var yPos = 4 + 38 * i;

	if (HSInfoView.Prefs.LinienTyp_BUS & LineRichtung.linienType) {
		LineIconLV = Ti.UI.createLabel({
			wordWrap : false,
			borderRadius : 4,
			backgroundColor : 'red',
			//borderColor : 'red',
			//borderWidth :2,
			color : 'white',
			font : {
				fontSize : 21,
				fontFamily : 'sans-serif'
			},
			textAlign : Ti.UI.TEXT_ALIGNMENT_CENTER,
			verticalAlign : Ti.UI.TEXT_VERTICAL_ALIGNMENT_CENTER,
			top : yPos,
			left : 5,
			width : 54,
			height : 28, //Titanium.UI.SIZE ,
			html : '<b>' + LineRichtung.linienNr + '</b>'
		});
	}

	if (HSInfoView.Prefs.LinienTyp_UBAHN & LineRichtung.linienType) {

		LinienImage = PREFS.LinienImagesDir + '/' + LineRichtung.linienNr + '_54_28.png';

		if (!Titanium.Filesystem.getFile(LinienImage).exists()) {
			Ti.API.error("*** Image not found:" + LinienImage);
			LinienImage = PREFS.LinienImagesDir + '/x_54_28.png';
			//TODO Nicht gefunden bild erstelle
		}

		LineIconLV = Ti.UI.createImageView({
			image : LinienImage,
			top : yPos,
			left : 5,
			height : 28,
			width : 54
		})
	};

	if (HSInfoView.Prefs.LinienTyp_SBAHN & LineRichtung.linienType) {

		LinienImage = PREFS.LinienImagesDir + '/s' + LineRichtung.linienNr + '_54_28.png';

		if (!Titanium.Filesystem.getFile(LinienImage).exists()) {
			Ti.API.error("*** Image not found:" + LinienImage);
			LinienImage = PREFS.LinienImagesDir + '/x_54_28.png';
			//TODO Nicht gefunden bild erstellen
		}

		LineIconLV = Ti.UI.createImageView({
			image : LinienImage,
			top : yPos,
			left : 5,
			height : 28,
			width : 54
		})
	};

	var HSLinieRichtungV = Ti.UI.createScrollView({
		contentWidth : 'auto',
		contentHeight : 'auto',
		showVerticalScrollIndicator : false,
		showHorizontalScrollIndicator : false,
		height : 30,
		left : 2 * HSInfoView.Prefs.HSICONLEFT + HSInfoView.Prefs.HSLINIENICONWIDTH,
		width : HSInfoView.Prefs.WIDTH - (2 * HSInfoView.Prefs.HSICONLEFT + HSInfoView.Prefs.HSLINIENICONWIDTH) - HSInfoView.Prefs.HSICONLEFT - (2 * HSInfoView.Prefs.HSDISTANCEBOXWIDTH) - 3 - HSInfoView.Prefs.HSICONDISTANCE,
		top : yPos,
		backgroundColor : '#333333',
		scrollType : 'horizontal'
	});

	if (CntAbfahrtzeiten == 1)
		HSLinieRichtungV.width = HSInfoView.Prefs.WIDTH - (1 * HSInfoView.Prefs.HSICONLEFT + HSInfoView.Prefs.HSLINIENICONWIDTH) - HSInfoView.Prefs.HSICONLEFT - (1 * HSInfoView.Prefs.HSDISTANCEBOXWIDTH) - 3 - HSInfoView.Prefs.HSICONDISTANCE

	var HSLinieRichtung = Ti.UI.createLabel({
		wordWrap : false,
		color : 'white',
		font : {
			fontSize : 20,
			fontFamily : 'sans-serif',
		},
		text : LineRichtung.richtung,
		textAlign : Ti.UI.TEXT_ALIGNMENT_LEFT,
		top : 0,
		left : 0,
		width : 'auto',
		height : 30,
	});

	if (CntAbfahrtzeiten == 2) {

		HSTrainArrival1 = Ti.UI.createLabel({
			wordWrap : false,
			borderRadius : 4,
			backgroundColor : getColorAbfahrtsBox(LineRichtung.abfahrten[0].abfahrtzeitOffset, this.HalteStelleDistanceM),
			color : 'white',
			font : {
				fontSize : 20,
				fontFamily : 'sans-serif'
			},
			textAlign : Ti.UI.TEXT_ALIGNMENT_CENTER,
			top : yPos - 2,
			left : HSInfoView.Prefs.WIDTH - HSInfoView.Prefs.HSICONLEFT - (2 * HSInfoView.Prefs.HSDISTANCEBOXWIDTH) - HSInfoView.Prefs.HSICONDISTANCE,
			width : HSInfoView.Prefs.HSDISTANCEBOXWIDTH,
			height : HSInfoView.Prefs.HSDISTANCEBOXHIGHT,
			html : formatAbfahrtszeit(((!this.IsRelativAbfahrtzeit) ? LineRichtung.abfahrten[0].abfahrtzeitRealTime : LineRichtung.abfahrten[0].abfahrtzeitOffset), this.IsRelativAbfahrtzeit, this.AbfahrtzeitOffset)
		});

		HSTrainArrival2 = Ti.UI.createLabel({
			wordWrap : false,
			borderRadius : 4,
			backgroundColor : getColorAbfahrtsBox(LineRichtung.abfahrten[1].abfahrtzeitOffset, this.HalteStelleDistanceM),
			color : 'white',
			font : {
				fontSize : 20,
				fontFamily : 'sans-serif'
			},
			textAlign : Ti.UI.TEXT_ALIGNMENT_CENTER,
			top : yPos - 2,
			left : HSInfoView.Prefs.WIDTH - HSInfoView.Prefs.HSICONLEFT - HSInfoView.Prefs.HSDISTANCEBOXWIDTH,
			width : HSInfoView.Prefs.HSDISTANCEBOXWIDTH,
			height : HSInfoView.Prefs.HSDISTANCEBOXHIGHT,
			html : formatAbfahrtszeit(((!this.IsRelativAbfahrtzeit) ? LineRichtung.abfahrten[1].abfahrtzeitRealTime : LineRichtung.abfahrten[1].abfahrtzeitOffset), this.IsRelativAbfahrtzeit, this.AbfahrtzeitOffset)
		});

		scrollView.add(HSTrainArrival1);
		scrollView.add(HSTrainArrival2);
	}

	if (CntAbfahrtzeiten == 1) {

		HSTrainArrival1 = Ti.UI.createLabel({
			wordWrap : false,
			borderRadius : 4,
			backgroundColor : getColorAbfahrtsBox(LineRichtung.abfahrten[0].abfahrtzeitOffset, this.HalteStelleDistanceM),
			color : 'white',
			font : {
				fontSize : 20,
				fontFamily : 'sans-serif'
			},
			textAlign : Ti.UI.TEXT_ALIGNMENT_CENTER,
			top : yPos - 2,
			left : HSInfoView.Prefs.WIDTH - HSInfoView.Prefs.HSICONLEFT - HSInfoView.Prefs.HSDISTANCEBOXWIDTH,
			width : HSInfoView.Prefs.HSDISTANCEBOXWIDTH,
			height : HSInfoView.Prefs.HSDISTANCEBOXHIGHT,
			html : formatAbfahrtszeit(((!this.IsRelativAbfahrtzeit) ? LineRichtung.abfahrten[0].abfahrtzeitRealTime : LineRichtung.abfahrten[0].abfahrtzeitOffset), this.IsRelativAbfahrtzeit, this.AbfahrtzeitOffset)
		});

		scrollView.add(HSTrainArrival1);
	}

	/*
	 abfahrtzeit	"21:31"
	 abfahrtzeitOffset	"12"
	 abfahrtzeitRealTime	"21:31"
	 */

	HSLinieRichtungV.add(HSLinieRichtung);

	scrollView.add(HSLinieRichtungV);

	scrollView.add(LineIconLV);

}
function formatAbfahrtszeit(pAbfahrtszeit, isRelativZeit, pAbfahrtzeitOffset) {


	if (!isRelativZeit) {
		return '<i><b>' + pAbfahrtszeit + '</b></i>';
	}

	pAbfahrtzeitOffset = pAbfahrtzeitOffset || 0;

	// is relativ
	var az = parseInt(pAbfahrtszeit, 10);
	if (az === 'NaN') {
		Ti.API.error("*** Abfahrtszeit keine Zahl:" + pAbfahrtszeit + " in formatAbfahrtszeitRelativ");
		return '<i><b>Err</b></i>';
	} else {
		az = az + (pAbfahrtzeitOffset/60/1000);
		Ti.API.info("----------->" + az);
		if (az <= 60 && az >=-60) {
			return '<i><b>' + az + '"' + '</b></i>';
		} else {
			var h = Math.floor(az / 60);
			var min = az % 60;
			if (('' + min).length == 1) {
				min = '0' + min;
			}
			return '<i><b>' + h + ':' + min + '</b></i>';
		}
	}
}

function getColorAbfahrtsBox(pAbfahrtszeitRelativ, pHalteSelleDistanzinMeter) {
	var distance = parseInt(pHalteSelleDistanzinMeter, 10);
	var ankunftReal = parseInt(pAbfahrtszeitRelativ, 10);

	var bedarfMin = distance / PREFS.GehGeschwindigkeitMeterinMinute;

	if (bedarfMin < ankunftReal * 0.975) {
		return '#3F913F';
		// gruen
	}

	if (bedarfMin < ankunftReal * 1.15) {
		return '#CFCC11';
		// gelb
	}

	return '#E0310D';
	// rot
}

function formatHalteStelleDistanz(pHalteStelleDistanz) {

	var az = parseInt(pHalteStelleDistanz, 10);
	if (az === 'NaN') {
		Ti.API.error("*** pHalteStelleDistanz keine Zahl:" + pHalteStelleDistanz + " in formatHalteStelleDistanz");
		return '<i><b>Err</b></i>';
	} else {
		if (az <= 2500) {
			return '<i><b>' + az + 'm' + '</b></i>';
		} else {

			if (az > 10000) {
				return '<i><b>&gt;10km</b></i>'
			}
			var h = az / 1000;
			h *= 10;
			h = Math.round(h);
			h /= 10;
			return '<i><b>' + h + 'km</b></i>';
		}
	}
	return pHalteStelleDistanz;
}

function createURL(pHalteStellenArray, pHSType, pAbfahrtzeitOffset) {
	
	pAbfahrtzeitOffset = pAbfahrtzeitOffset || 0;
	
	var url = 'http://geolessiprovider.appspot.com/getnxtab?';
	var hs = "";
	for (var i = 0; i < pHalteStellenArray.length; i++) {
		hs = hs + 'hs=' + pHalteStellenArray[i] + '&';
	}
		
	var time = new Date().getTime() + pAbfahrtzeitOffset  - (20*1000);
	
	return url + hs + "time=" + time + '&hstype=' + pHSType;
}

function createLoadingScreeen() {

	var nobouncingview = Titanium.UI.createView({
		backgroundColor : '#333333',
		left : (HSInfoView.Prefs.WIDTH / 2) - (65 / 2),
		top : 60,
		height : 65,
		width : 65
	});

	var a = Titanium.UI.createWebView({
		showScrollbars : false,
		left : 0,
		top : 0,
		width : 80,
		height : 80,
		html : '<html><body style="margin:0px; padding:0px; background-color:#333333"><img width="42" height="42" src="http://geolessiprovider.appspot.com/ajax-loader.gif" /></body></html>',
		focusable : false,
		loading : false,
		touchEnabled : false,
		enableZoomControls : false,
		scalesPageToFit : false,
		backgroundColor : '#333333',
		disableBounce : true
	});

	nobouncingview.add(a);
	return nobouncingview;
}

module.exports = WSCaller;
